package com.spread.libserver.controller;

import com.spread.libserver.doer.dofactory.Operation;
import com.spread.libserver.model.constant.AccountType;
import com.spread.libserver.model.dao.Book;
import com.spread.libserver.model.network.BookResponse;
import com.spread.libserver.model.network.CategoryResponse;
import com.spread.libserver.model.network.Response;

import org.springframework.web.bind.annotation.*;


/**
 * All operations that a manager can do.
 */
@RestController
public class ManagerController {

    @GetMapping("managerop/getallcat")
    public CategoryResponse getAllCategoriesManager(){
        return Operation.getAllCategories();
    }

    @GetMapping("managerop/addcategory")
    public Response addCategory(@RequestParam("name") String name,
                                @RequestParam(value = "pname", required = false) String pname){
        return Operation.addCategory(name, pname);
    }


    // TODO: Foreign key constraint, cannot delete category with books.
    @GetMapping("managerop/deletecategory")
    public Response deleteCategory(@RequestParam("name") String name){
        return Operation.deleteCategory(name);
    }

    @GetMapping("managerop/addbook")
    public Response addBook(@RequestParam("isbn") String ISBN,
                            @RequestParam(value = "name", required = false) String name,
                            @RequestParam(value = "author", required = false) String author,
                            @RequestParam(value = "publisher", required = false) String publisher,
                            @RequestParam(value = "summary", required = false) String summary,
                            @RequestParam(value = "cover", required = false) String cover,
                            @RequestParam(value = "price", required = false) Float price,
                            @RequestParam(value = "stock", required = false) Integer stock,
                            @RequestParam(value = "category", required = false) String category){
        return Operation.addBook(ISBN, name, author, publisher,
                summary, cover, price, stock, category);
    }

    @GetMapping("managerop/deletebook")
    public Response deleteBook(@RequestParam("isbn") String ISBN){
        return Operation.deleteBook(ISBN);
    }

    @GetMapping("managerop/getbook/bycategory")
    public BookResponse getBookByCategoryManager(@RequestParam("category") String category){
        return Operation.getBookByCategory(category);
    }

    @GetMapping("managerop/getbook/byname")
    public BookResponse getBookByNameManager(@RequestParam("name") String name){
        return Operation.getBookByName(name);
    }

    @PostMapping("managerop/updatebook")
    public Response updateBook(@RequestBody Book newBook){
        return Operation.updateBook(newBook);
    }

    @GetMapping("managerop/register")
    public Response registerManager(@RequestParam("account") String acc,
                                    @RequestParam("password") String pwd){
        return Operation.register(acc, pwd, AccountType.MANAGER);
    }


    @GetMapping("managerop/login")
    public Response loginManager(@RequestParam("account") String acc,
                              @RequestParam("password") String pwd){
        return Operation.login(acc, pwd);
    }

    @GetMapping("managerop/changepassword")
    public Response changePasswordManger(@RequestParam("account") String acc,
                                   @RequestParam("target") String targetAcc,
                                   @RequestParam("newpswd") String newPassword){
        return Operation.changePassword(acc, targetAcc, newPassword, AccountType.MANAGER);
    }




}
